package com.lile.regex;

import java.util.Arrays;

/*
 * 分割功能
 *		String类的功能：
 *			public String[] split(String regex)
 */
public class RegexSplit {

	public static void main(String[] args) {
		splitTest2();
	}

	public static void splitTest1() {
		// aa,bb,cc
		String s1 = "aa,bb,cc";
		String regex1 = ",";
		String[] strArray1 = s1.split(regex1);
		for (int x = 0; x < strArray1.length; x++) {
			System.out.println(strArray1[x]);
		}
		System.out.println("---------------");
		// aa.bb.cc
		String s2 = "aa.bb.cc";
		String regex2 = "\\.";
		String[] strArray2 = s2.split(regex2);
		for (int x = 0; x < strArray2.length; x++) {
			System.out.println(strArray2[x]);
		}
		System.out.println("---------------");
		// aa bb cc
		String s3 = "aa bb cc";
		String regex3 = " ";
		String[] strArray3 = s3.split(regex3);
		for (int x = 0; x < strArray3.length; x++) {
			System.out.println(strArray3[x]);
		}
		System.out.println("---------------");
		// aa bb cc
		String s4 = "aa             bb    cc";
		String regex4 = " +";
		String[] strArray4 = s4.split(regex4);
		for (int x = 0; x < strArray4.length; x++) {
			System.out.println(strArray4[x]);
		}
		System.out.println("---------------");
		// D:\develop\eclipse-SDK-3.7.2-win64\workspace\day14_Regex
		String s5 = "D:\\develop\\eclipse-SDK-3.7.2-win64\\workspace\\day14_Regex";
		String regex5 = "\\\\";
		String[] strArray5 = s5.split(regex5);
		for (int x = 0; x < strArray5.length; x++) {
			System.out.println(strArray5[x]);
		}
		System.out.println("---------------");
	}

	/*
	 * 我有如下一个字符串:”91 27 46 38 50” 请写代码实现最终输出结果是：”27 38 46 50 91”
	 * 
	 * 分析： A:定义一个字符串 B:按照空格进行分割字符串，得到一个字符串数组 C:把字符串数组转换为整数数组 D:对整数数组进行排序
	 * E:把排序后的整数数组转换成字符串
	 */
	public static void splitTest2() {
		// 定义一个字符串
		String s = "91 27 46 38 50";

		// 按照空格进行分割字符串，得到一个字符串数组
		String[] strArray = s.split(" ");

		// 把字符串数组转换为整数数组
		int[] arr = new int[strArray.length];
		for (int x = 0; x < arr.length; x++) {
			arr[x] = Integer.parseInt(strArray[x]);
		}

		// 对整数数组进行排序
		Arrays.sort(arr);

		// 把排序后的整数数组转换成字符串
		StringBuilder sb = new StringBuilder();
		for (int x = 0; x < arr.length; x++) {
			if (x == arr.length - 1) {
				sb.append(arr[x]);
			} else {
				sb.append(arr[x]).append(" ");
			}
		}
		String result = sb.toString();

		System.out.println(result);
	}

}
